<?php

function experts_work_list_page()
{
    @$output = drupal_render(drupal_get_form('experts_work_list_form'));
    return $output;
}

function experts_work_list_form($form, &$form_state)
{
    if (!empty($_GET['expert_id'])) {
        $form_state['expert_id'] = $_GET['expert_id'];
    }
    if (!empty($form_state['expert_id'])) {
        $form['#expert_id'] = $form_state['expert_id'];
    }
    $init_year =  !empty($form_state['values']['year'])?$form_state['values']['year']:"";
    $init_month = !empty($form_state['values']['month'])?$form_state['values']['month']:"";
    _insert_date_selection_form($form, $form_state, 5,$init_year,$init_month);
     $form['title_fuzzy'] = array(
        '#type' => 'textfield',
        '#title' => '标题',
        '#default_value' => !empty($form_state['values']['title_fuzzy'])?$form_state['values']['title_fuzzy']:"",
        '#maxlength' => 50,
    );
    $form['content_fuzzy'] = array(
        '#type' => 'textfield',
        '#title' => '内容',
        '#default_value' => !empty($form_state['values']['content_fuzzy'])?$form_state['values']['content_fuzzy']:"",
        '#maxlength' => 100,
    );
    $form['attendees_fuzzy'] = array(
        '#type' => 'textfield',
        '#title' => '参与人',
        '#default_value' => !empty($form_state['values']['attendees_fuzzy'])?$form_state['values']['attendees_fuzzy']:"",
        '#maxlength' => 100,
    );
    $form['experts_name'] = array(
        '#type' => 'textfield',
        '#title' => '专家姓名',
        '#default_value' => !empty($form_state['values']['experts_name'])?$form_state['values']['experts_name']:"",
        '#id'=>'search-expert-name',
        '#maxlength' => 20,
    );
    $res = db_select('employee','E')->condition('E.department_id','4')->fields('E')->execute()->fetchAll();
    $option[''] ='请选择';
    foreach($res as $k=> $v){
        $option[$v->employee_id] =$v->employee_name;
    }
    $form['author_uid'] = array(
        '#type' => 'select',
        '#title' => '填报人',
        '#options' => $option,
        '#default_value' => !empty($form_state['values']['attendees_fuzzy'])?$form_state['values']['attendees_fuzzy']:"",
    );
    $table['#markup'] = data_list_view_page($form, $form_state);
    ajax_submit_form($form, $form_state, $table, '');

    return $form;
}

function experts_work_list_form_validate($form, &$form_state)
{
}

function data_list_view_page(&$form = '', &$form_state = '')
{
    global $user;
    $output = '';
    //表头
    $header = array(
        array('data' => '选择', 'class' => ['essential no']),
        array('data' => '地点', 'class' => ['essential more'],'field' => 'address'),
        array('data' => '标题', 'class' => ['essential more']),
        array('data' => '内容', 'class' => ['essential more']),
        array('data' => '参与人', 'class' => ['essential more']),
        array('data' => '日期', 'class' => ['essential more'],'field' => 'work_date'),
        array('data' => '操作', 'class' => ['essential more']),
    );
    $query = db_select('experts_work', 'V')->extend('PagerDefault')->extend('TableSort');
//    $query->join('experts','E','E.experts_id = V.experts_id');
    $datestr = '';
    if (!empty($form_state['values']['year'])) {//查询条件
        if ($form_state['values']['year']) {
            $datestr .= $form_state['values']['year'] . '-';
        }
    }
    if (!empty($form_state['values']['month'])) {//查询条件
        if ($month = $form_state['values']['month']) {
            if ($month < 10)
                $datestr .= '0' . $form_state['values']['month'];
            else
                $datestr .= $form_state['values']['month'];
        }
    }
    if(isset($form['#expert_id']) && !empty($form['#expert_id'])){
        $query->condition('V.experts_id',$form['#expert_id'], '=');
    }
    if ($datestr) {
        $query->condition('V.work_date', db_like($datestr) . '%', 'LIKE');
    }
    if (!empty($form_state['values']['title_fuzzy'])) {//查询条件
        $query->condition('V.title', '%'.db_like($form_state['values']['title_fuzzy']) . '%', 'LIKE');
    }
    if (!empty($form_state['values']['content_fuzzy'])) {//查询条件
        $query->condition('V.content', '%'.db_like($form_state['values']['content_fuzzy']) . '%', 'LIKE');
    }
    if (!empty($form_state['values']['attendees_fuzzy'])) {//查询条件
        $query->condition('V.attendees', '%'.db_like($form_state['values']['attendees_fuzzy']) . '%', 'LIKE');
    }
    if(!empty($form_state['values']['author_uid'])){
        $query->condition('V.author_uid',$form_state['values']['author_uid'], '=');
    }
    if(!user_access('view all experts_work') && user_access('view own experts_work')){
        $query->condition('V.author_uid',$user->uid, '=');
    }

    if(!empty($form_state['values']['experts_name'])){
        $query->condition('V.experts_id',get_id_from_string_field('experts','experts_id','experts_name',$form_state['values']['experts_name'],false), '=');
    }
    $query->fields('V');
    $statQuery = $query->cloneQuery();
    $statQuery->addExpression('count(*)','count');
    $statInfo = $statQuery->execute()->fetchObject();
    $form["#stat"]['#count'] = $statInfo->count;

    if(empty($form_state['values']) || empty($form_state['values']['order'])) {
        $query->orderBy('V.timestamp', 'DESC');
    }
    $query->limit(variable_get("list_page_item_number", 5));
    $experts_workInfo = $query->orderByHeader($header)->execute();
    $rows = array();
    //表内容
    $n = 1;
    foreach ($experts_workInfo as $row) {
        if (nonghua_user_access('experts_work', 'delete', $row->experts_work_id, $row->author_uid, 0)) {
            $disabled = ' class="listViewTableCheck" ';
        } else{
            $disabled = ' disabled="true" ';
        }
        if(!empty($form['#expert_id'])){
            $str = '?expert_id='.$form['#expert_id'];
            $url_str = '&expert_id='.$form['#expert_id'];
        }else{
            $str = '';
            $url_str = '';
        }
        $rows[] = array(
            array('data' => "<input type='checkbox'".$disabled." value ='" . $row->experts_work_id . "' name='" . $row->experts_work_id . "'/>" . $n++, 'class' => 'integrant essential no'),
            array('data' => $row->address, 'class' => ['essential distributor']),
            array('data' => $row->title, 'class' => ['essential distributor']),
            array('data' => $row->content, 'class' => ['essential distributor']),
            array('data' => $row->attendees, 'class' => ['essential distributor']),
            array('data' => $row->work_date, 'class' => ['essential more']),
            array('data' => '<a href="' . base_path() . 'experts_work/edit/' . $row->experts_work_id .$str. '"  data-toggle="modal" target="_self"><img width="20px" height="20px" src="' . base_path() . 'sites/all/themes/tweme/images/images/cz.png"/></a>', 'class' => 'essential more'),
        );
    }

    $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('没有记录'), 'attributes' => array('class' => array('table table-bordered table-striped table-hover enhanced')))); //表格
    /*	 * *按钮** */
    if (sizeof($rows) > 0 && (user_access('delete own experts_work') ||  user_access('delete all experts_work'))) {
        $output .= '<a href="javascript:void(0)" id="listViewTableSelectAll" data-toggle="modal" class="btn btn-mini">全选</a>';
        $output .= '<a href="javascript:void(0)" id="listViewTableDeleteSelected" data-toggle="modal" class="btn btn-mini">删除所选</a>';
    }

     $output .= '<a href="'.base_path().'experts_work/export?&year='.$form_state['values']['year'].'&month='.$form_state['values']['month'].'&title_fuzzy='.$form_state['values']['title_fuzzy'].'&content_fuzzy='.$form_state['values']['content_fuzzy'].'&attendees_fuzzy='.$form_state['values']['attendees_fuzzy'].'&author_uid='.$form_state['values']['author_uid'].$url_str.'" data-toggle="modal" class="btn btn-mini">导出</a>';
    $output .= theme('pager');
    return $output;
}

function experts_work_delete()
{
    ajax_table_delete('experts_work', 'experts_work_id');
}

function experts_work_export($form,&$form_state){
    $letter = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
    $tableheader = array('地点','标题','内容','参与人','日期','参与专家','上报人');
    $data = get_experts_work_data($form, $form_state);
    nonghua_php_excel($letter,$tableheader,$data,'专家记录数据');
}

function get_experts_work_data($form = '', &$form_state = ''){
    global $user;
    $user_info = _get_user_info($user->uid);
    $query = db_select('experts_work', 'V');
    $query->join('experts','E','V.experts_id = E.experts_id');
    $query->join('employee','EP','V.author_uid = EP.employee_id');
    $datestr = '';
    if (!empty($_GET['year'])) {//查询条件
        if ($_GET['year']) {
            $datestr .= $_GET['year'] . '-';
        }
    }
    if (!empty($_GET['month'])) {//查询条件
        if ($month = $_GET['month']) {
            if ($month < 10)
                $datestr .= '0' . $_GET['month'];
            else
                $datestr .= $_GET['month'];
        }
    }
    if ($datestr) {
        $query->condition('V.work_date', db_like($datestr) . '%', 'LIKE');
    }
    if (!empty($_GET['title_fuzzy'])) {//查询条件
        $query->condition('V.title', '%'.db_like($_GET['title_fuzzy']) . '%', 'LIKE');
    }
    if (!empty($_GET['content_fuzzy'])) {//查询条件
        $query->condition('V.content', '%'.db_like($_GET['content_fuzzy']) . '%', 'LIKE');
    }
    if (!empty($_GET['attendees_fuzzy'])) {//查询条件
        $query->condition('V.attendees', '%'.db_like($_GET['attendees_fuzzy']) . '%', 'LIKE');
    }
    if(!empty($_GET['author_uid'])){
        $query->condition('V.author_uid',$_GET['author_uid'], '=');
    }
    if(!user_access('view all experts_work') && user_access('view own experts_work')){
        $query->condition('V.author_uid',$user->uid, '=');
    }
    if(isset($_GET['expert_id']) && !empty($_GET['expert_id'])){
        $query->condition('V.experts_id',$_GET['expert_id'], '=');
    }
    $query->fields('V',array('address','title','content','attendees','work_date'));
    $query->addField('E','experts_name');
    $query->addField('EP','employee_name');
    $experts_workInfo = $query->execute()->fetchAll();
    return $experts_workInfo;
}